//
//  TabBarController.m
//  
//
//  Created by Maxwell Rose on 15/9/2.
//
//

#import "TabBarController.h"
#import "NavigationController.h"
#import "HomeViewController.h"
#import "FindViewController.h"
#import "MineViewController.h"

@interface TabBarController ()<UITabBarControllerDelegate>

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    HomeViewController *home = [[HomeViewController alloc] init];
    [self addController:home title:@"首页" image:@"tab_menu1_nor" seletedImage:@"tab_menu1_sel"];
    
    FindViewController *find = [[FindViewController alloc] init];
    [self addController:find title:@"发现" image:@"tab_menu2_nor" seletedImage:@"tab_menu2_sel"];
    
    MineViewController *mine = [[MineViewController alloc] init];
    [self addController:mine title:@"我的" image:@"tab_menu4_nor" seletedImage:@"tab_menu4_sel"];
    
    self.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)addController:(UIViewController *)controller title:(NSString *)title image:(NSString *)image seletedImage:(NSString *)seletedImage
{
    controller.title = title;
    controller.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:seletedImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    NSDictionary *seletedTextAtts = @{
                                      NSForegroundColorAttributeName:kColorGreenTheme

                                      };
    
    NSDictionary *textAtts = @{
                               NSForegroundColorAttributeName:UIColorFromRGB(0x757575)
                               };
    
    [controller.tabBarItem setTitleTextAttributes:textAtts forState:(UIControlStateNormal)];
    [controller.tabBarItem setTitleTextAttributes:seletedTextAtts forState:(UIControlStateSelected)];
    
    
    
    NavigationController *navigation = [[NavigationController alloc] initWithRootViewController:controller];
    [self addChildViewController:navigation];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NavigationController *nav = (NavigationController *)viewController;
    
    if([nav.topViewController isKindOfClass:[MineViewController class]] || [nav.topViewController isKindOfClass:[FindViewController class]])
    {
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"token"] != nil)
            return TRUE;
        else
        {
//            LoginViewController *loginViewCon = [[LoginViewController alloc]init];
//            [self presentViewController:loginViewCon animated:TRUE completion:^{}];
            return FALSE;
        }
        
    }
    return TRUE;
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
